from selenium.webdriver.chrome.options import Options
from selenium import webdriver

'''
selenium相关
driver下载:https://npm.taobao.org/mirrors/chromedriver/
版本对照:https://blog.csdn.net/BinGISer/article/details/88559532
'''
class SeleniumHelper:
    '''
    无头浏览器处理业务逻辑
    '''
    def __init__(self):
        chrome_opt = Options()  # 创建参数设置对象.
        chrome_opt.add_argument('--headless')  # 无界面化.
        chrome_opt.add_argument('--disable-gpu')  # 配合上面的无界面化.
        chrome_opt.add_argument('--window-size=1366,768')  # 设置窗口大小, 窗口大小会有影响.
        prefs = {
            'profile.default_content_setting_values': {
                'images': 2
            }
        }
        chrome_opt.add_experimental_option('prefs', prefs)
        # 创建Chrome对象并传入设置信息.
        self.driver = webdriver.Chrome(options=chrome_opt)
    def findPrice(self, url):
        '''
        获取产品页面价格
        '''
        # driver = webdriver.Chrome()     # 创建Chrome对象.

        self.driver.get(url)
        # print(url)
        self.driver.implicitly_wait(3)  # seconds

        myDynamicElement = self.driver.find_element_by_xpath('//*[@class="mainprice"]')
        # text = self.driver.page_source
        # print(myDynamicElement.text)
        price= myDynamicElement.text
        price = price.replace('¥', '')
        ps = price.split('-')
        price = ps[0].replace(' ', '')
        # print(price)
        return float(price)
    def getHtml(self,url):
        self.driver.implicitly_wait(10)  # seconds
        self.driver.get(url)
        text = self.driver.page_source
        return text
